package ms.mall.controller;

import ms.common.exception.BizException;
import ms.common.util.string.StringUtil;
import ms.mall.bean.UserAddrParamBean;
import ms.mall.config.ResponseCode;
import ms.mall.entity.UserAddr;
import ms.mall.service.AddrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * Project Name: ms
 * File Name: AddrController
 * Package Name: ms.mall.controller
 * Date: 2018/6/4  15:35
 * Copyright (c) 2018, tianyul All Rights Reserved.
 */
@RestController
@RequestMapping("/addr")
public class AddrController {

    @Autowired
    private AddrService addrService;

    /**
     * 新增收货地址
     * @param userAddrParamBean
     * @return
     */
    @PostMapping(value = "/create")
    public void create(@RequestBody UserAddrParamBean userAddrParamBean) {
        checkParam(userAddrParamBean);
        addrService.create(userAddrParamBean);
    }

    /**
     * 修改收货地址
     * @param userAddrParamBean
     * @return
     */
    @PostMapping(value = "/update")
    public void update(@RequestBody UserAddrParamBean userAddrParamBean) {
        checkParam(userAddrParamBean);
        addrService.update(userAddrParamBean);
    }

    /**
     * 删除收货地址
     * @param userAddrParamBean
     * @return
     */
    @PostMapping(value = "/del")
    public void del(@RequestBody UserAddrParamBean userAddrParamBean) {
        addrService.del(userAddrParamBean);
    }

    /**
     * 查看收货地址
     * @param userAddrParamBean
     * @return
     */
    @PostMapping(value = "/detail")
    public UserAddr detail(@RequestBody UserAddrParamBean userAddrParamBean) {
        return addrService.detail(userAddrParamBean);
    }

    /**
     * 查看收货地址列表
     * @param userAddrParamBean
     * @return
     */
    @PostMapping(value = "/list")
    public List<UserAddr> list(@RequestBody UserAddrParamBean userAddrParamBean) {
        return addrService.list(userAddrParamBean);
    }

    /**
     * 校验参数
     * @param userAddrParamBean
     */
    private void checkParam(UserAddrParamBean userAddrParamBean) {
        if(StringUtil.isEmpty(userAddrParamBean.getOpenid())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "用户不存在");
        }
        if(StringUtil.isEmpty(userAddrParamBean.getUserName())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "收件人不能为空");
        }
        if(StringUtil.isEmpty(userAddrParamBean.getTel())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "电话不能为空");
        }
        if(StringUtil.isEmpty(userAddrParamBean.getAddr())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "地址不能为空");
        }
        if(StringUtil.isEmpty(userAddrParamBean.getProvinceId())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "省份不能为空");
        }
        if(StringUtil.isEmpty(userAddrParamBean.getProvinceName())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "省份不能为空");
        }
        if(StringUtil.isEmpty(userAddrParamBean.getCityId())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "市区不能为空");
        }
        if(StringUtil.isEmpty(userAddrParamBean.getCityName())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "市区不能为空");
        }
        if(StringUtil.isEmpty(userAddrParamBean.getAreaId())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "地区不能为空");
        }
        if(StringUtil.isEmpty(userAddrParamBean.getAreaName())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "地区不能为空");
        }
    }

}
